package com.eqdd.yiqidian.ui.gongneng.renshi.train;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.TrainPlanActivityCustom;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.TrainPlanListBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.utils.RefreshUtil;
import com.eqdd.yiqidian.utils.SlimUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.jakewharton.rxbinding.view.RxView;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.PostRequest;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author吕志豪 .
 * @date 17-12-5  下午2:10.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 */

public class TrainPlanListActivity extends BaseActivity {

    private TrainPlanActivityCustom dataBinding;
    private SlimAdapterEx<TrainPlanListBean> slimAdapterEx;
    private int type = 2;
    private boolean isSelect;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_train_plan_list);
        initTopTitleBar(View.VISIBLE, "培训计划");
        initTopRightText("添加", v -> startActivityForResult(AddTrainPlanActivity.class, null, Config.ADD_TRAIN_PLAN));
    }

    @Override
    public void initData() {
        isSelect = getIntent().getBooleanExtra(Config.IS_SELECT, false);
        RefreshUtil.openRefresh(dataBinding.coolRefreshView, coolRefreshView -> {
            refresh(0);
        });
        dataBinding.spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (type != position + 2) {
                    if (position + 2 == 4) {
                        changeAnim(dataBinding.rlMoney, dataBinding.etSearch);
                    } else if (type == 4) {
                        changeAnim(dataBinding.etSearch, dataBinding.rlMoney);
                    }
                }
                type = position + 2;
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        slimAdapterEx = SlimAdapterEx.create().attachTo(dataBinding.recyclerView).updateData(new ArrayList());
        SlimUtil.layout(slimAdapterEx, R.layout.list_item_12, TrainPlanListBean.class);
        ItemClickSupport.addTo(dataBinding.recyclerView)
                .setOnItemClickListener((recyclerView, position, v) -> {
                    if (isSelect) {
                        Intent intent = new Intent();
                        intent.putExtra(Config.BEAN, slimAdapterEx.getDataItem(position));
                        setResult(Config.SUCCESS, intent);
                        finish();
                    } else {
                        Bundle bundle = new Bundle();
                        bundle.putInt(Config.ID, slimAdapterEx.getDataItem(position).getId());
                        startActivity(TrainPlanInfoActivity.class, bundle);
                    }
                });
        RxView.clicks(dataBinding.ivBt)
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribe(aVoid -> {
                    if (type != 4 && !TextUtils.isEmpty(dataBinding.etSearch.getText())) {
                        refresh(type, dataBinding.etSearch.getText().toString());
                    } else if (type == 4 && !TextUtils.isEmpty(dataBinding.etMin.getText()) && !TextUtils.isEmpty(dataBinding.etMax.getText())) {
                        refresh(type, dataBinding.etMin.getText().toString().trim(), dataBinding.etMax.getText().toString().trim());
                    }
                });
        dataBinding.coolRefreshView.setRefreshing(true);
    }

    private void changeAnim(View apprence, View disapper) {
        if (apprence.getVisibility() == View.GONE) {
            ViewCompat.setAlpha(apprence, 0);
            apprence.setVisibility(View.VISIBLE);
            ValueAnimator valueAnimator = ValueAnimator.ofInt(0, 100);
            valueAnimator.setDuration(300);
            valueAnimator.addUpdateListener(animation -> {
                int value = (int) animation.getAnimatedValue();
                ViewCompat.setAlpha(apprence, value * 0.01f);
                ViewCompat.setAlpha(disapper, 1 - value * 0.01f);
            });
            valueAnimator.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {

                }

                @Override
                public void onAnimationEnd(Animator animation) {

                    disapper.setVisibility(View.GONE);

                }

                @Override
                public void onAnimationCancel(Animator animation) {

                }

                @Override
                public void onAnimationRepeat(Animator animation) {

                }
            });
            valueAnimator.start();
        }
    }

    @Override
    public void setView() {

    }

    private void refresh(int type) {
        refresh(type, "");
    }

    private void refresh(int type, String content) {
        refresh(type, content, "", "");
    }

    private void refresh(int type, String themin, String themax) {
        refresh(type, "", themin, themax);
    }

    private void refresh(int type, String content, String themin, String themax) {

        PostRequest<HttpResult<List<TrainPlanListBean>>> params = OkGo.<HttpResult<List<TrainPlanListBean>>>post(HttpConfig.BASE_URL + HttpConfig.TRAIN_PLAN_LIST)
                .params("userGuid", AppConstent.user.getGuid())
                .params("comid", AppConstent.user.getCompanyId());
        switch (type) {
            case 2:
                params.params("theTheme", content);
                break;
            case 3:
                params.params("teacherName", content);
                break;
            case 4:
                params.params("themin", themin);
                params.params("themax", themax);
                break;
            case 5:
                params.params("theDepName", content);
                break;
            case 6:
                params.params("theTrainees", content);
                break;

        }
        params.params("type", type)
                .execute(new DialogCallBack<HttpResult<List<TrainPlanListBean>>>(TrainPlanListActivity.this) {
                    @Override
                    public void onSuccess(Response<HttpResult<List<TrainPlanListBean>>> response) {
                        HttpResult<List<TrainPlanListBean>> httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            slimAdapterEx.updateData(httpResult.getItems());
                        }
                        dataBinding.coolRefreshView.setRefreshing(false);
                    }

                    @Override
                    public void onError(Response<HttpResult<List<TrainPlanListBean>>> response) {
                        super.onError(response);
                        dataBinding.coolRefreshView.setRefreshing(false);
                    }
                });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Config.ADD_TRAIN_PLAN && resultCode == Config.REFRESH) {
            refresh(0, "");
        }
    }
}
